package com.yztc.love_travel.ui.mainservice.historyculture.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.yztc.love_travel.R;
import com.yztc.love_travel.adater.HistoryCultureAdapter;
import com.yztc.love_travel.bean.HistoryBean;
import com.yztc.love_travel.constant.OtherConstant;
import com.yztc.love_travel.constant.URLConstant;
import com.yztc.love_travel.ui.mainservice.historyculture.HistoryDetailActivity;
import com.yztc.love_travel.ui.mainservice.historyculture.presenter.HistoryCulturePresenterImpl;
import com.yztc.love_travel.ui.mainservice.historyculture.presenter.IHistoryCulturePresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 历史文化
 */
public class HistoryCultureActivity extends AppCompatActivity implements IHistoryCultureView {
    private IHistoryCulturePresenter mPresenter;
    @BindView(R.id.history_toolbar)
    Toolbar historyToolbar;
    @BindView(R.id.listview_history)
    ListView listviewHistory;
    @BindView(R.id.activity_history_culture)
    LinearLayout activityHistoryCulture;
    private List<HistoryBean.ManualArticleListBean> manualleList;
    private HistoryCultureAdapter mHistoryCultureAdapter;
    private Context mContext=this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_culture);
        ButterKnife.bind(this);
        initPresenter();
        loadData();
        initToolBar();
        initAdapter();
    }

    private void loadData() {
        Map<String,Object> params = new HashMap<>();
        params.put(URLConstant.HistoryCulture.PAGENO,0);
        params.put(URLConstant.HistoryCulture.PAGESIZE,20);
        params.put(URLConstant.HistoryCulture.CATE,1);
        params.put(URLConstant.HistoryCulture.STATUS,1);
        mPresenter.getHistoryCultureBean(URLConstant.HistoryCulture.URL_HISTORY_BASE,params);
    }
    private void initPresenter() {
        mPresenter=new HistoryCulturePresenterImpl(this);
    }
    private void initAdapter() {
        manualleList=new ArrayList<>();
        mHistoryCultureAdapter=new HistoryCultureAdapter(manualleList,mContext);
        listviewHistory.setAdapter(mHistoryCultureAdapter);
        listviewHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(HistoryCultureActivity.this, HistoryDetailActivity.class);
                int idl =manualleList.get(position).getId();
                intent.putExtra(OtherConstant.KEY_HISTORY_ID,idl);
                startActivity(intent);

            }
        });
    }
    private void initToolBar() {
        historyToolbar.setTitle(R.string.history_culture);
        setSupportActionBar(historyToolbar);
        ActionBar action=getSupportActionBar();
        action.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void loadStart() { }

    @Override
    public void loadFinish() {}

    @Override
    public void loadError() {}

    @Override
    public void showRegionCultureData(HistoryBean historyCultureBean) {
        List<HistoryBean.ManualArticleListBean> manualArticleList = historyCultureBean.getManualArticleList();
        manualleList.addAll(manualArticleList);
        mHistoryCultureAdapter.notifyDataSetChanged();
    }
}
